﻿using DataServices;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LFWeb3.Controllers
{
    public class ArtistController : Controller
    {
        DaoArtist ArtistDao = new DaoArtist();
        DaoArtGroup ArtGroupDao = new DaoArtGroup();

        public ActionResult Artist()
        {
            IList<ArtistInfo> Artists = new List<ArtistInfo>();
            Artists = ArtistDao.LoadAllArtists();
            ViewBag.Artist = Artists;
            return View();
        }
        public ActionResult Add()
        {
            ViewBag.ArtGroup = ArtGroupDao.LoadAllArtGroup();
            return View();
        }

        public ActionResult Edit()
        {
            ViewBag.Artist = ArtistDao.LoadAllArtists();
            return View();
        }

        public ActionResult Update(string id)
        {
            ArtistInfo Artist = ArtistDao.LoadArtistByID(id);
            ViewBag.Artist = Artist;

            ViewBag.ArtGroup = ArtGroupDao.LoadAllArtGroup();
            return View();
        }

        public ActionResult ViewArtist(string id)
        {
            IList<ArtistInfo> Artists = new List<ArtistInfo>();
            Artists = ArtistDao.LoadAllArtists();
            ViewBag.Artists = Artists;
            ViewBag.Artist = ArtistDao.LoadArtistByID(id);
            return View();
        }

        [ValidateInput(false)]
        public JsonResult AddArtist(ArtistInfo Artist)
        {
            try
            {
                Artist.ArtistID = Guid.NewGuid().ToString();
                ArtistDao.AddArtist(Artist);
                return Json("success", JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(ex.Message, JsonRequestBehavior.AllowGet);
            }
        }

        [ValidateInput(false)]
        public JsonResult DeleteArtist(string id)
        {
            try
            {
                ArtistDao.DeleteArtist(id);
                return Json("success", JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(ex.Message, JsonRequestBehavior.AllowGet);
            }
        }

        [ValidateInput(false)]
        public JsonResult UpdateArtist(ArtistInfo Artist)
        {
            try
            {
                ArtistDao.UpdateArtist(Artist);
                return Json("success", JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(ex.Message, JsonRequestBehavior.AllowGet);
            }
        }

    }
}
